package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     *
     * @param dto
     */
    @Override
    @Transactional
    public void save(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);

        setmealMapper.save(setmeal);

        //添加套餐菜品信息
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //返回套餐id
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.save(setmealDish);
        }

    }

    /**
     * 套餐分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO dto) {
        //分页插件
        PageHelper.startPage(dto.getPage(), dto.getPageSize());

        Page<SetmealVO> vos = setmealMapper.page(dto);

        return new PageResult(vos.getTotal(), vos.getResult());
    }

    /**
     * 根据ID批量删除套餐
     *
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            SetmealVO vo = setmealMapper.query(id);
            //在售状态不可删除
            if (vo.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            //套餐删除同时删除关联菜品
            setmealMapper.delete(id);
            setmealDishMapper.delete(id);
        }
    }

    /**
     * 根据ID查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO query(Long id) {
        //查询数据
        SetmealVO vo = setmealMapper.query(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.query(id);
        //封装数据
        vo.setSetmealDishes(setmealDishes);

        return vo;
    }

    /**
     * 修改套餐
     *
     * @param dto
     */
    @Override
    public void updateId(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.update(setmeal);

        //先删除所有套餐菜品
        setmealDishMapper.delete(setmeal.getId());

        //新增菜品
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()) {
            for (SetmealDish setmealDish : setmealDishes) {
                //设置套餐ID
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.save(setmealDish);
            }
        }

    }

    /**
     * 套餐起售、停售
     *
     * @param status
     * @param id
     */
    @Override
    public void fuckStatus(Integer status, Long id) {
        List<SetmealDish> setmealDishes = setmealDishMapper.query(id);
        for (SetmealDish setmealDish : setmealDishes) {
            //获取菜品信息
            Long dishId = setmealDish.getDishId();
            Dish dish = dishMapper.getById(dishId);
            //判断菜品状态
            if (dish.getStatus() == StatusConstant.DISABLE) {
                //套餐内包含未启售菜品,抛出异常
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        //更新数据
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
